﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Collections.ObjectModel;

namespace TestClient
{
    public partial class MainPage : UserControl
    {
        ObservableCollection<DeviceEventService.DeviceEventData> eventData;

        public MainPage()
        {
            InitializeComponent();

            eventData = new ObservableCollection<DeviceEventService.DeviceEventData>();

            EndpointAddress address = new EndpointAddress("http://localhost:8000/DeviceEventService");

            CustomBinding binding = new CustomBinding(
                new PollingDuplexBindingElement(),
                new BinaryMessageEncodingBindingElement(),
                new HttpTransportBindingElement());

            DeviceEventService.DeviceEventServiceClient client = new DeviceEventService.DeviceEventServiceClient(binding, address);
            client.SendDeviceEventDataReceived += new EventHandler<DeviceEventService.SendDeviceEventDataReceivedEventArgs>(client_SendDeviceEventReceived);
            client.ConnectAsync(Guid.NewGuid().ToString());
            EventListBox.ItemsSource = eventData;
        }

        void client_SendDeviceEventReceived(object sender, DeviceEventService.SendDeviceEventDataReceivedEventArgs e)
        {
            if (!eventData.Contains(e.data))
            {
                eventData.Add(e.data);
            }
        }
    }
}
